package com.example.controller;

import com.example.common.Result;
import com.example.entity.MonitoringSite;
import com.example.service.MonitoringSiteService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Collections;

@RestController
@RequestMapping("/site")
@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
public class MonitoringSiteController {
    private final MonitoringSiteService monitoringSiteService;

    public MonitoringSiteController(MonitoringSiteService monitoringSiteService) {
        this.monitoringSiteService = monitoringSiteService;
    }

    @GetMapping
    public Result<List<MonitoringSite>> getAllSites() {
        List<MonitoringSite> sites = monitoringSiteService.getAllSites();
        return Result.success(sites);
    }

    @DeleteMapping("/{siteId}")
    public Result<Void> deleteSite(@PathVariable Integer siteId) {
        if (monitoringSiteService.deleteSite(siteId)) {
            return Result.success(null);
        }
        return Result.error("删除失败");
    }

    @PostMapping
    public Result<Void> addSite(@RequestBody MonitoringSite site) {
        // 验证必要参数
        if (site.getRoomId() == null) {
            return Result.error("房间ID不能为空");
        }
        if (site.getSiteName() == null || site.getSiteName().trim().isEmpty()) {
            return Result.error("站点名称不能为空");
        }
        
        // 检查同一房间内是否有重名站点
        List<MonitoringSite> existingSites = monitoringSiteService.getSitesByRoomId(site.getRoomId());
        boolean hasDuplicate = existingSites.stream()
                .anyMatch(s -> s.getSiteName().equals(site.getSiteName()));
        if (hasDuplicate) {
            return Result.error("该房间已存在同名站点");
        }

        if (monitoringSiteService.addSite(site)) {
            return Result.success(null);
        }
        return Result.error("添加失败");
    }

    @PutMapping
    public Result<Void> updateSite(@RequestBody MonitoringSite site) {
        // 验证必要参数
        if (site.getSiteId() == null) {
            return Result.error("站点ID不能为空");
        }
        
        // 获取原有站点信息
        MonitoringSite existingSite = monitoringSiteService.getSiteById(site.getSiteId());
        if (existingSite == null) {
            return Result.error("站点不存在");
        }
        
        // 如果修改了站点名称，检查是否重名
        if (!existingSite.getSiteName().equals(site.getSiteName())) {
            List<MonitoringSite> roomSites = monitoringSiteService.getSitesByRoomId(existingSite.getRoomId());
            boolean hasDuplicate = roomSites.stream()
                    .filter(s -> !s.getSiteId().equals(site.getSiteId()))
                    .anyMatch(s -> s.getSiteName().equals(site.getSiteName()));
            if (hasDuplicate) {
                return Result.error("该房间已存在同名站点");
            }
        }
        
        // 保持原有的不可修改字段
        site.setRoomId(existingSite.getRoomId());
        site.setInstalledAt(existingSite.getInstalledAt());
        
        if (monitoringSiteService.updateSite(site)) {
            return Result.success(null);
        }
        return Result.error("更新失败");
    }

    @GetMapping("/{siteId}")
    public Result<List<MonitoringSite>> getSiteById(@PathVariable Integer siteId) {
        MonitoringSite site = monitoringSiteService.getSiteById(siteId);
        if (site != null) {
            return Result.success(Collections.singletonList(site));
        }
        return Result.error("未找到该站点");
    }

    @GetMapping("/room/{roomId}")
    public Result<List<MonitoringSite>> getSitesByRoomId(@PathVariable Integer roomId) {
        List<MonitoringSite> sites = monitoringSiteService.getSitesByRoomId(roomId);
        if (sites != null && !sites.isEmpty()) {
            return Result.success(sites);
        }
        return Result.error("未找到该房间的站点");
    }
} 